import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : '检测中',
  dataLists: [],
  Fields: [],
};
const actions = {
  //待检列表
  getLists({commit}, page) {
    request.get(Server.action.stock_checkStock, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.forEach(function (item, i) {
              item.dataIndex = item.key
              item.align = 'center';
              // if(i < fields.length-1) {
                item.width = item.width || "10%"
              // }
            })
            commit("setFields", fields);
        }
    })
  },
  //库存检测
  testing({commit}, page) {
    commit("setErrorCode", '检测中');
    request.post(Server.action.stock_checkStatus, page )
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setErrorCode", response.data.result);
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
